package com.imau.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imau.bean.Role;
import com.imau.common.entiry.ResultCode;
import com.imau.common.entiry.ResultEntity;
import com.imau.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@RequestMapping("/role")
@RestController
public class RoleController   {


    @Autowired
    private RoleService roleService ;


    /**
     * 添加
     * @param role
     * @throws ServletException
     * @throws IOException
     */
    @PostMapping
    public ResultEntity add(@RequestBody  Role role)  {


        int row = roleService.save(role);

        return  ResultEntity.writeResult( row > 0 ? true : false);

    }


    /**
     * 删除
     *
     * @throws ServletException
     * @throws IOException
     */
    @DeleteMapping("/{id}")
    public ResultEntity removeById(@PathVariable String id)   {

        //删除
        int row = roleService.deleteById(id);
        //4.返回响应

       return  ResultEntity.writeResult( row > 0 ? true : false);


    }


    @PutMapping
    public ResultEntity edit(@RequestBody Role role)   {




        //3.保存
        int row = roleService.updateRole(role);

        //4.返回响应

       return  ResultEntity.writeResult( row > 0 ? true : false);

    }


    /**
     * @param request
     * @param role
     * @throws ServletException
     * @throws IOException
     */

    @GetMapping
    public ResultEntity list(HttpServletRequest request, Role role) throws ServletException, IOException {



        String currentPage = request.getParameter("currentPage");

        String pageSize = request.getParameter("pageSize");


        IPage<Role> page = roleService.queryListByPagte(role,Integer.valueOf(currentPage),Integer.valueOf(pageSize));


        return new ResultEntity(ResultCode.SUCCESS, page);


    }


    @GetMapping({"/{id}"})
    public ResultEntity getInfo(@PathVariable String id)  {





        Role role = roleService.findById(id);

        //4.返回响应
       return new ResultEntity(ResultCode.SUCCESS, role);


    }

    @GetMapping("/assignPermission/{id}")

    public ResultEntity assignPermission(@PathVariable String id,  String[] perids) throws ServletException, IOException {

        //角色Id





        int row = roleService.assignPermission(id, perids);


        //4.返回响应
        return  ResultEntity.writeResult( row > 0 ? true : false);


    }
}
